﻿using Ku.AppCore.Events;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using System.Resources;
using Ku.Modules.VideoTool.Utils;

namespace Ku.Modules.VideoTool.ViewModels
{
    public partial class VideoCompressionViewModel : ViewModelBase
    {
        private readonly IEventAggregator _eventAggregator;
        public VideoCompressionViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }

        #region 属性
        [ObservableProperty]
        private string _SourceDirectory = "源目录";
        [ObservableProperty]
        private string _ResultDirectory = "结果目录";
        #endregion

        #region 事件



        /// <summary>
        /// 选择源目录
        /// </summary> 
        [RelayCommand]
        private void SelectedSourcePathClick(object parameter)
        {
            System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
            // 设置初始目录
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.UserProfile;
            var isShowDialog = folderBrowserDialog.ShowDialog();          // 这个方法可以显示文件夹选择对话框
            if (isShowDialog == System.Windows.Forms.DialogResult.OK)
            {
                // 获取选择的文件夹的全路径名
                string directoryPath = folderBrowserDialog.SelectedPath;
                SourceDirectory = directoryPath;
            }
        }
        /// <summary>
        /// 选择源目录
        /// </summary> 
        [RelayCommand]
        private void SelectedResultPathClick(object parameter)
        {
            System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
            // 设置初始目录
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.UserProfile;
            var isShowDialog = folderBrowserDialog.ShowDialog();          // 这个方法可以显示文件夹选择对话框
            if (isShowDialog == System.Windows.Forms.DialogResult.OK)
            {
                // 获取选择的文件夹的全路径名
                string directoryPath = folderBrowserDialog.SelectedPath;
                ResultDirectory = directoryPath;
            }
        }
        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="parameter"></param>
        [RelayCommand]
        private void StartClick(object parameter)
        {
            try
            {
                new Thread(o =>
                {

                    VideoCompression();

                }).Start();

            }
            catch (Exception ex)
            {

                throw;
            }
        }


        private async void VideoCompression()
        {
            msg("开始转换。。。");
            try
            {
                string inputPath = @"D:\Administrator\视频\压缩工具测试\原视频\210.mp4";
                string outputPath = $@"D:\Administrator\视频\压缩工具测试\结果视频\210_{DateTime.Now.ToString("yyyyMMddHHmmss ")}.mp4";


                //var inputFile = new MediaFile { Filename = @"D:\Administrator\视频\压缩工具测试\原视频\210.mp4" };
                //var outputFile = new MediaFile { Filename = @"D:\Administrator\视频\压缩工具测试\结果视频\210.mp4" };

                //using (var engine = new Engine())
                //{
                //    engine.GetMetadata(inputFile);

                //    var options = new ConversionOptions
                //    {
                //        VideoBitRate = 1000,
                //        VideoSize = VideoSize.Hd720
                //    };

                //    engine.Convert(inputFile, outputFile, options);
                //} 

                FFmpegUtil fFmpegUtil = new FFmpegUtil();
                //fFmpegUtil.VideoCompression(inputPath, outputPath);
                fFmpegUtil.Cmd(inputPath, outputPath);
            }
            catch (Exception ex)
            {

                msg("转换失败。。。" + ex.ToString());
            }
            msg("转换完毕。。。");
        }

        #endregion

        #region 私有方法

        private void msg(string message)
        {
            try
            {
                _eventAggregator.GetEvent<MessageSentEvent>().Publish(message);

            }
            catch (Exception ex)
            {

            }
        }
        #endregion
    }
}
